****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************
	
clear all
set more off

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"


use all-lottery-movers-analysis.dta , clear 
keep if (year>2008 & ksample & stayer==1)

gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1
	
gen frl_diff = frl_mb_at - frl_mb_as
gen white_diff = white_mb_at - white_mb_as
gen aig_diff = aig_at - aig_as
gen lep_diff = lep_Y_at - lep_Y_as
gen schoolva_diff = schoolva_at - schoolva_as
gen teststand_as = 0.5*mathstand_as + 0.5*readstand_as
gen teststand_at = 0.5*mathstand_at + 0.5*readstand_at
gen teststand_diff = teststand_at - teststand_as

xtset lottery

tempfile ksample 
	save `ksample' 

global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"

xtreg frl_diff $outcomecats $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store A

xtreg frl_diff $outcomecats teststand_diff $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store B

xtreg white_diff $outcomecats $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store C

xtreg white_diff $outcomecats teststand_diff $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store D

esttab A B C D using tableA13.tex  , replace ///
mtitles("No Qual." "Cond Scores" "No Qual." "Cond Scores" ) ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" teststand_diff "\emph{School Quality Measure}" , nolabel ) ///
label booktabs nonum gaps f noobs collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp teststand_diff) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS" teststand_diff "Change in Level Scores") 

esttab A B C D using tableA13.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))
  
** sixth grade 
use all-lottery-movers-analysis.dta , clear 
keep if (year>2008 & sixsample & stayer==1)

gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

gen frl_diff = frl_mb_at - frl_mb_as
gen white_diff = white_mb_at - white_mb_as
gen aig_diff = aig_at - aig_as
gen lep_diff = lep_Y_at - lep_Y_as
gen schoolva_diff = schoolva_at - schoolva_as
gen teststand_as = 0.5*mathstand_as + 0.5*readstand_as
gen teststand_at = 0.5*mathstand_at + 0.5*readstand_at
gen teststand_diff = teststand_at - teststand_as

xtset lottery

tempfile sixsample 
	save `sixsample' 
	
global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"

xtreg frl_diff $outcomecats $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store A

xtreg frl_diff $outcomecats teststand_diff $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store B

xtreg white_diff $outcomecats $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store C

xtreg white_diff $outcomecats teststand_diff $lotcons , fe cluster(lottery)
estadd scalar N_ind = e(N)
est store D

esttab A B C D using tableA14.tex  , replace ///
mtitles("No Qual." "Cond Scores" "No Qual." "Cond Scores") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" teststand_diff "\emph{School Quality Measure}" , nolabel ) ///
label booktabs nonum gaps f noobs collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp teststand_diff) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS" teststand_diff "Change in Level Scores") 

esttab A B C D using tableA14.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))
  
 



	
